package com.example.demo.testWeb;

import org.apache.commons.io.FileUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;

/**
 * 〈〉
 */
@Controller
public class TestWebController {


    private final TestWebService testWebService;

    public TestWebController(TestWebService testWebService) {
        this.testWebService = testWebService;
    }

    @PostMapping("/aa")
    public ResponseEntity<?> testResponseEntity(MultipartFile file) throws IOException {
        return testWebService.testResponseEntity(file);
    }


    @GetMapping("/cc")
    public ResponseEntity<?> cc() throws UnsupportedEncodingException, FileNotFoundException {
        return testWebService.cc();
    }


    @GetMapping("/bb")
    public String testReturnHtml() {
        return "hellow";
    }
    @RequestMapping("/upload")
    public void upload(MultipartFile file, HttpServletRequest request) {
        try {
            //获取原始文件名
            String filename = file.getOriginalFilename();
            //图片保存创建图片路径
            String uploadPath = request.getSession().getServletContext().getRealPath("static/upload");
            //非空
            if (!file.isEmpty()) {
                //通过一个路径创建文件，斜杠可以用File.separator代替
                File saveFile = new File(uploadPath + File.separator + file.getOriginalFilename());
                //将文件保存到本地1需要一个流2需要保存的文件
                FileUtils.copyInputStreamToFile(file.getInputStream(), saveFile);
                //将文件路径保存到数据库
               /* String sqlPath = "/static/upload/" + file.getOriginalFilename();
                return describeService.saveImage((String) JSONObject.fromObject(ks).get("ks"), sqlPath);*/
            }
        } catch (Exception e) {
            ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @RequestMapping("/down")
    public ResponseEntity<?> testResponseEntity(HttpSession session, MultipartFile file) {
//region 读取项目文件
        /*        byte [] body = null;
        ServletContext servletContext = session.getServletContext();
        InputStream in = servletContext.getResourceAsStream("/files/abc.txt");
        body = new byte[in.available()];
        in.read(body);
        return    ResponseEntity
                .ok()
                .header("Content-Disposition", "attachment;filename=abc.txt")
                .body(body);
        */
//endregion


        //region 本地文件
        BufferedReader reader = null;
//        BufferedReader reader= new BufferedReader(new InputStreamReader(new FileInputStream("D:/aa.txt"), "UTF-8"));
        BufferedReader bufferedReader = null;
        try {
//            bufferedReader= new BufferedReader(new InputStreamReader(file.getInputStream(), "UTF-8"));
            bufferedReader = new BufferedReader(new FileReader("D:/aa.txt"));
            StringBuilder stringBuilder = new StringBuilder();
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }

            return ResponseEntity
                    .ok()
//                    .contentType(MediaType.APPLICATION_JSON)
                    .header("Content-Disposition", "attachment;filename=aa.txt")
                    .body(stringBuilder.toString());
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } finally {
            try {
                bufferedReader.close();
            } catch (Exception e) {
                return ResponseEntity.badRequest().body(e.getMessage());
            }

        }
    }

    //endregion
}
